package 设计模式.创建型模式.抽象工厂模式;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.File;
import java.io.IOException;

/**
 * @author MT
 * @date 2019-11-13 17:05
 */
public class ReadXML1 {

    public static Object getObject() {

        try {
            DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
            Document doc = documentBuilder.parse(new File("src/main/java/设计模式/创建型模式/抽象工厂模式/config1.xml"));
            NodeList nl = doc.getElementsByTagName("className");
            Node firstChild = nl.item(0).getFirstChild();
            String cName = "设计模式.创建型模式.抽象工厂模式." + firstChild.getNodeValue();
            Class<?> aClass = Class.forName(cName);
            Object o = aClass.newInstance();
            return o;
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }
        return null;
    }
}
